package org.bbelovic.devel.exercises.sequences.impl;

import java.util.ArrayList;
import java.util.List;

import org.bbelovic.devel.exercises.sequences.SequenceGenerator;

public class LookAndSaySequenceGenerator implements SequenceGenerator {

    public List<Integer> generateSequence(int n) {

        List<Integer> result = new ArrayList<Integer>();
        String prev = null;
        String act = null;
        int counter = 0;
        StringBuilder sb = new StringBuilder();
        sb.append("1");
        result.add(Integer.valueOf(sb.toString()));
        for (int i = 1; i < n; i++) {
            StringBuilder newSb = new StringBuilder();
            for (int j = 0; j < sb.length() ;j++) {
                act = sb.substring(j, j+1);
                if (prev == null || act.equals(prev)) {
                    counter++;
                    prev = act;
                } else {
                    newSb.append(counter).append(prev);
                    counter = 1;
                    prev = act;
                }
            }
            newSb.append(counter).append(prev);
            sb = new StringBuilder(newSb.toString());
            result.add(Integer.valueOf(sb.toString()));
            counter = 0;
            prev = null;
        }
        return result;
    }

}
